package com.ssyc.gsk_teacher_appraisal.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ssyc.common.base.Constants;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.PopUpManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.SPUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.common.view.dialog.CustomDialogManager;
import com.ssyc.gsk_teacher_appraisal.R;
import com.ssyc.gsk_teacher_appraisal.activity.TkTeacherAppraisalQuestionActivity;
import com.ssyc.gsk_teacher_appraisal.bean.TableInfo;
import com.ssyc.gsk_teacher_appraisal.constant.TeacherAppKeys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 表格展示学生分析列表的适配器
 * Created by Administrator on 2018/10/9 0009.
 */

public class TableRvAdapter extends BaseQuickAdapter<TableInfo.ListBean, BaseViewHolder> {

    private Context context;
    private Activity activity;
    private List<TableInfo.ListBean> oldDatas;

    public TableRvAdapter(Context context, Activity activity, int layoutResId, @Nullable List<TableInfo.ListBean> data) {
        super(layoutResId, data);
        this.context = context;
        this.oldDatas = data;
        this.activity = activity;
    }

    @Override
    protected void convert(BaseViewHolder helper, TableInfo.ListBean item) {

        //线条
        View line = helper.getView(R.id.line);
        if (helper.getLayoutPosition() == 0) {
            line.setVisibility(View.GONE);
        } else {
            line.setVisibility(View.VISIBLE);
        }

        //排名
        TextView tvPm = helper.getView(R.id.tv_pm);
        tvPm.setText((helper.getLayoutPosition() + 1) + "");

        //姓名
        TextView tvName = helper.getView(R.id.tv_name);
        tvName.setText(item.getName());

        //分数
        TextView tvfs = helper.getView(R.id.tv_fs);
        if (TextUtils.isEmpty(item.getScore())) {
            tvfs.setText("-");
        } else {
            tvfs.setText(item.getScore());
        }


        //提交时间
        TextView tvTime = helper.getView(R.id.tv_tjsj);
        if (TextUtils.isEmpty(item.getDone_time())) {
            tvTime.setText("-");
        } else {
            tvTime.setText(item.getDone_time());
        }

        //错题数
        TextView tvCts = helper.getView(R.id.tv_cts);
        if (TextUtils.isEmpty(item.getWrong_num())) {
            tvCts.setText("-");
        } else {
            tvCts.setText(item.getWrong_num());
        }

        //查看试卷情况
        TextView tv_sjqk = helper.getView(R.id.tv_sjqk);
        if (TextUtils.isEmpty(item.getDone_time())) {
            tv_sjqk.setText("-");
        } else {
            tv_sjqk.setText("查看");
            tv_sjqk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (TextUtils.isEmpty(item.getDone_time())) {
                        //没有做过的状态
                        UiUtils.Toast("该学生还未参加该试卷测评,不允许查看", false);
                        return;
                    } else {
                        // 3老师查看某个学生的成绩和学生查看自己的成绩(34号接口)
                        SPUtil.put(context, TeacherAppKeys.AUTHERTYPE, 3);
                        //跳转到查看详情的界面
                        Intent intent = new Intent(context, TkTeacherAppraisalQuestionActivity.class);
                        intent.putExtra("rowId", item.getRow_id2());
                        intent.putExtra("h5rowid", item.getRow_id());
                        intent.putExtra("exam_id", item.getExam_id());
                        intent.putExtra("school_id", item.getSchool_id());
                        intent.putExtra("class_num", item.getClass_num());
                        intent.putExtra("student", item.getStudent());
                        intent.putExtra("name", item.getName());
                        context.startActivity(intent);
                    }
                }
            });
        }

        //操作
        TextView tvCz = helper.getView(R.id.tv_cz);
        tvCz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //弹推送框
                showPushPop(item);

                //请求接口进行推送操作
//                doPushAction(item);
            }
        });
    }


    /**
     * 弹推送框
     */
    private void showPushPop(TableInfo.ListBean item) {
        PopUpManager.showPop(context, R.layout.teacher_app_pop_push, 0.3f, activity, new PopUpManager.onGetViewListener() {
            @Override
            public void getChildView(View view, int layoutResId) {
                //内容
                TextView tvContent = view.findViewById(R.id.tv_content);
                tvContent.setText("确定再次推送试卷给" + item.getName() + "吗?");

                //取消
                TextView tvCancel = view.findViewById(R.id.tv_cancel);
                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PopUpManager.dismiss();
                    }
                });

                //提交
                TextView tvCommit = view.findViewById(R.id.tv_commit);
                tvCommit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PopUpManager.dismiss();
                        //提交操作
                        doPushAction(item);
                    }
                });

            }
        });
    }


    /**
     * 再次推送
     */
    private void doPushAction(TableInfo.ListBean item) {
        CustomDialogManager.show(context, "推送中...");
        Map<String, String> map = new HashMap<>();
        map.put("type", "5");
        map.put("ttel", item.getTeacher());
        map.put("row_id", item.getRow_id());

        HttpUtils.post(HttpAdress.PUSHAGAIN, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                CustomDialogManager.dissmiss();
                UiUtils.Toast("网络异常,推送失败", false);
            }

            @Override
            public void onResponse(String response, int id) {
                CustomDialogManager.dissmiss();

                if (!TextUtils.isEmpty(response)) {
                    TableInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, TableInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            //返回成功
                            List<TableInfo.ListBean> newDatas = info.getList();
                            if (newDatas != null) {
                                //刷新页面数据
                                oldDatas.clear();
                                oldDatas.addAll(newDatas);
                                notifyDataSetChanged();
                                UiUtils.Toast("推送成功", false);
                            }
                        } else {
                            UiUtils.Toast(Constants.ERRORSTATE + info.getState(), false);
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }


//                if (!TextUtils.isEmpty(response)) {
//                    ResultInfo info = null;
//                    try {
//                        info = GsonUtil.jsonToBean(response, ResultInfo.class);
//                    } catch (Exception e) {
//                        Log.i("test", Constants.PARSEDATAERROR);
//                    }
//                    if (null != info) {
//                        if ("200".equals(info.getState())) {
//                            //返回成功
//                            UiUtils.Toast("推送成功", false);
//                        } else {
//                            UiUtils.Toast(Constants.ERRORSTATE + info.getState(), false);
//                            Log.i("test", Constants.ERRORSTATE + info.getState());
//                        }
//
//                    } else {
//                        Log.i("test", Constants.EMPTY);
//                    }
//
//                }
            }
        });
    }
}
